﻿using System;
using System.Collections.Generic;

namespace SR.ExpressionEvaluator.Lib.Core.Functions
{
    internal class If : IFunction
    {
        List<object> _Args = new List<object>();

        public List<object> Args
        {
            get
            {
                return _Args;
            }
            set
            {
                _Args = value;
            }
        }

        public object Evaluate()
        {
            if (!Validate())
            {
                throw new Exception("IF: Validation failed");
            }

            if (Convert.ToBoolean(Args[0]))
            {
                return Args[1];
            }
            else
            {
                return Args[2];
            }
        }

        public bool Validate()
        {
            if (Args.Count != 3)
            {
                return false;
            }
            return true;
        }

        public string GetDefinition()
        {
            return @"IF(<logical test>,<TRUE cond.>,<FALSE cond.>)";
        }
    }
}
